package com.data.modules.system.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.data.modules.system.entity.MagicApiFile;
import com.data.modules.system.mapper.MagicApiFileMapper;
import com.data.modules.system.service.IMagicApiFileService;
import com.data.modules.system.vo.MagicPathVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: magic_api_file
 * @Author: data-boot
 * @Date:   2022-04-06
 * @Version: V1.0
 */
@Service
public class MagicApiFileServiceImpl extends ServiceImpl<MagicApiFileMapper, MagicApiFile> implements IMagicApiFileService {
    @Autowired
    private MagicApiFileMapper magicApiFileMapper;
    //@Value("${server.servlet.context-path}")
    //String path;

    @Override
    public List<MagicPathVO> queryAllPath(String fileName,String fileUrl) {
        ArrayList<MagicPathVO> magicPathVOS = new ArrayList<>();
        QueryWrapper<MagicApiFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().ne(MagicApiFile::getFileContent,"this is directory").orderByDesc(MagicApiFile::getId);
        List<MagicApiFile> magicApiFiles = magicApiFileMapper.selectList(queryWrapper);
        magicApiFiles.forEach(a->{

            if(a.getFilePath().endsWith(".ms")){
                MagicPathVO magicPathVO = new MagicPathVO();
                StringBuilder stringBuilder = new StringBuilder();
                String msUrl="";
                // mg 说不要/jeecg-boot
                //stringBuilder.append(path);
               JSONObject fileContent = JSONObject.parseObject(a.getFileContent().substring(0,a.getFileContent().indexOf("===========")));
               String path = fileContent.getString("path");
               if(!path.startsWith("/")){
                    msUrl="/"+path;
               }else {
                   msUrl=path;
               }
               //进行匹配 .ms 匹配 .json
               for (MagicApiFile magicApiFile : magicApiFiles) {
                   if(magicApiFile.getFilePath().endsWith(".json")){
                       String filePath = a.getFilePath().substring(0, a.getFilePath().lastIndexOf("/"));
                       if(magicApiFile.getFilePath().contains(filePath)){
                           JSONObject file = JSONObject.parseObject(magicApiFile.getFileContent());
                           String path2 = file.getString("path");
                           String jsUrl="";
                           if(!path2.startsWith("/")){
                                jsUrl="/"+path2;
                           }else {
                               jsUrl=path2;
                           }
                           stringBuilder.append(jsUrl);
                       }

                   }
               }
                magicPathVO.setFileUrl(stringBuilder.append(msUrl).toString());
                magicPathVO.setFileName(fileContent.get("name").toString());
                magicPathVO.setFilePath(a.getFilePath());
                magicPathVO.setId(String.valueOf(a.getId()));
                magicPathVO.setMethod(ObjectUtil.isEmpty(fileContent.get("method")) ? "GET" : fileContent.get("method").toString());
                magicPathVO.setJsonStr("fileName:"+magicPathVO.getFileName()
                        +"|filePath:"+magicPathVO.getFilePath()
                        +"|fileUrl:"+magicPathVO.getFileUrl());
                magicPathVOS.add(magicPathVO);
           }
        });
        if(ObjectUtil.isNotEmpty(fileName)){
            List<MagicPathVO> pathVOS = magicPathVOS.stream().filter(a -> a.getFileName().contains(fileName)).collect(Collectors.toList());
            return pathVOS;
        }
        if(ObjectUtil.isNotEmpty(fileUrl)){
            String[] split = fileUrl.split(",");
            List<String> list = Arrays.asList(split);
            List<MagicPathVO> pathVOS = magicPathVOS.stream().filter(a -> list.contains(a.getFileUrl())).collect(Collectors.toList());
            return pathVOS;
        }
        return magicPathVOS;
    }
}
